<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\activity\model;

use app\cassification\model\ClassificationDictionary;
use app\Cnarea\model\Cnarea;
use plugin\saiadmin\basic\BaseModel;

/**
 * 活动列表模型
 */
class activity extends BaseModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'activity_id';

    /**
     * 数据库表名称
     * @var string
     */
    protected $table = 'eb_activity';
    protected $append = [
        'activity_category_id_text',
        'province_id_text',
        'city_id_text',
        'district_id_text',
        'activity_status_text',
    ];
    
    /**
     * 活动开始时间 搜索
     */
    public function searchActivityStartTimeAttr($query, $value)
    {
        $query->whereTime('activity_start_time', 'between', $value);
    }
    public function SearchActivityStatusAttr($query, $value)
    {
        
        if($value==2){
            $query->where('activity_end_time', '<=', date('Y-m-d H:i:s',time())); 
        }elseif($value==1){
            $query->where('activity_start_time', '<=', date('Y-m-d H:i:s',time())); 
        }elseif($value==0){
            $query->where('activity_start_time', '>', date('Y-m-d H:i:s',time()));
        }
       
    }
    
    /**
     * 活动结束时间 搜索
     */
    public function searchActivityEndTimeAttr($query, $value)
    {
        $query->whereTime('activity_end_time', 'between', $value);
    }
    
    /**
     * 活动标题 搜索
     */
    public function searchActivityTitleAttr($query, $value)
    {
        $query->where('activity_title', 'like', '%'.$value.'%');
    }

    public function getProvinceIdTextAttr($value,$data){
        if(isset($data['city_id'])){
            $value=Cnarea::where('area_code',$data['province_id'])->value('name');
            if(!$value)$value='全国';
        }else{
            $value='全国';
        }
        return $value;
    }
    public function getCityIdTextAttr($value,$data){
       
        if(isset($data['city_id'])){
            $value=Cnarea::where('area_code',$data['city_id'])->value('name');
            if(!$value)$value='全省';
        }else{
            $value='全省';
        }
      
        return $value;
    }
    
    public function getDistrictIdTextAttr($value,$data){
        if(isset($data['district_id'])){
            $value=Cnarea::where('area_code',$data['district_id'])->value('name');
            if(!$value)$value='全市';
        }else{
            $value='全市';
        }
        return $value;
    }
    public function getActivityCategoryIdTextAttr($value,$data)
    {
        if(isset($data['activity_category_id'])){
          return    ClassificationDictionary::where('dictionary_id', $data['activity_category_id'])->value('classification_name_zh');
        }
        return '';
    }
    public function getActivityStatusTextAttr($value,$data) {
        if(isset($data['activity_start_time']) && isset($data['activity_end_time'])){
            if($data['activity_end_time'] <= date('Y-m-d H:i:s',time())){
                return '已完成';
            }elseif($data['activity_start_time'] <=date('Y-m-d H:i:s',time())){
                return '进行中';
            }else{
                return '未开始';
            }
          }
          return '';
    }
    


}
